//
//  DataBaseHandle.m
//  WristWatch
//
//  Created by dllo on 15/11/2.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "DataBaseHandle.h"

@implementation DataBaseHandle

+ (instancetype)shareDataBaseHandle {
    static DataBaseHandle *handle;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        handle = [[DataBaseHandle alloc] init];
    });
    [handle openDB];
    return handle;
}

- (void)openDB {
    // 1. 沙盒路径
    NSString *sandPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    // 2. 拼接路径
    NSString *dbPath = [sandPath stringByAppendingPathComponent:@"MZYstyleModel.sqlite"];
    int result = sqlite3_open([dbPath UTF8String], &dbPoint);
    if (result == SQLITE_OK) {
        NSLog(@"打开成功");
    }else {
        NSLog(@"打开失败");
    }
    NSLog(@"%@", dbPath);
}

- (void)createTable {
    //    NSString *sqlStr = @"Create table if not exists movie(movieId text primary key, movieName text, picurl text)";
    
    NSString *sqlStr = @"Create table if not exists MZYstyleModel(image text, name text, product_id text)";
    
    // 执行sql语句
    int result = sqlite3_exec(dbPoint, [sqlStr UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"创建成功");
    }else {
        NSLog(@"创建失败");
    }
}

- (selectStyle)isHaveInTable:(MZYstyleModel *)style {
    // 除了openDB, 都先写语句
    //    NSString *sqlStr = [NSString stringWithFormat:@"select *from movie where movieId = '%@'", movie.movieId];
    
    NSString *sqlStr = [NSString stringWithFormat:@"select *from MZYstyleModel where product_id = '%@'", style.product_id];
    NSLog(@"%@", style.product_id);
    
    // 跟随指针
    sqlite3_stmt *stmt = nil;
    // 执行sql语句
    int result = sqlite3_prepare_v2(dbPoint, [sqlStr UTF8String], -1, &stmt, nil);
    if (result == SQLITE_OK) {
        NSLog(@"查询成功");
        if (sqlite3_step(stmt) == SQLITE_ROW) {
            sqlite3_finalize(stmt);
            return InTable;
        }else {
            sqlite3_finalize(stmt);
            return NotInTable;
        }
    } else {
        sqlite3_finalize(stmt);
        NSLog(@"查询失败");
        return SelectError;
    }
    
}

- (void)saveStyleInDB:(MZYstyleModel *)style {
    NSString *sqlStr = [NSString stringWithFormat:@"insert into MZYstyleModel (product_id,name,image) values ('%@','%@','%@')", style.product_id, style.name, style.image];
    int result = sqlite3_exec(dbPoint, [sqlStr UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"添加成功");
    } else {
        NSLog(@"添加失败");
    }
}

- (void)deleteStyleInDB:(MZYstyleModel *)style {
    //    NSString *sqlStr = [NSString stringWithFormat:@"delete from movie where movieId = '%@'", movie.movieId];
    NSString *sqlStr = [NSString stringWithFormat:@"delete from MZYstyleModel where product_id = '%@'", style.product_id];
    
    int result = sqlite3_exec(dbPoint, [sqlStr UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"删除成功");
    }
    else {
        NSLog(@"删除失败");
    }}

- (NSMutableArray *)selectAllStyle {
    NSMutableArray *styleArr = [NSMutableArray array];
    //
    NSString *sqlStr = @"select * from MZYstyleModel";
    //    NSString *sqlStr = @"selecr * from movie";
    // 跟随指针
    sqlite3_stmt * stmt = nil;
    // 执行查找
    int result = sqlite3_prepare_v2(dbPoint, [sqlStr UTF8String], -1, &stmt, nil);
    if (result == SQLITE_OK) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            MZYstyleModel *style = [[MZYstyleModel alloc] init];
            style.name = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 1)];
            style.product_id = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 2)];
            style.image = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 0)];
            [styleArr addObject:style];
            [style release];
        }
    }
    sqlite3_finalize(stmt);
    
    
    
    return styleArr;
}




@end
